<?php
/**
* @author Bimal Poudel
* @copyright since 2006, Bimal Poudel
* @package Smarty Framework
* @support http://www.odesk.com/users/~~dd91d11caed0cdce
* @contact http://www.sanjaal.com
* @company Sanjaal Corps
*/

# Created on: 2011-03-26 02:03:52 890

/**
* Edit an entity in [ posts ]
*/

$posts = new posts();

# Handle Editing, when data is supplied
if($variable->post('edit-action', 'string', '') && ($post_id = $variable->post('post_id', 'integer', 0)))
{
	# Editing....
	$code = $variable->post('protection_code', 'string', '');
	$data = $variable->post('posts', 'array', array());
	
	# Mark when this data was modified last time.
	$data['modified_on'] = 'UNIX_TIMESTAMP(CURRENT_TIMESTAMP())';
	
	if($success = $posts->edit(
		$data, # Posted data
		array(
			'post_id' => $post_id,
		),
		$code, # Security code related to this entry
		$post_id
	))
	{
		# Something about the image uploaders as a patch
		# $cu = new customized_uploader('uploader', __BASE__.'/templates/images/posts', 'images/posts', $record_id=$post_id);
		
		$messenger = new messenger('success', 'The record has been modified.');
		
		stopper::url(url::last_page('posts-list.php'));
		#stopper::url('posts-edit-successful.php');
		#stopper::url('posts-list.php');
	}
	else
	{
		stopper::url('posts-edit-error.php');
	}
}
else
{
	/**
	* Otherwise, load the details of the entity before editing it.
	*/
	if($post_id = $variable->get('id', 'integer', 0))
	{
		$details = $posts->details($post_id);
		if(!$details)
		{
			# Data about this entity was not available
			stopper::url('posts-edit-error.php');
		}
		# Purpose of this code block is to make sure that the variable
		# gets all indices with blank data, to feed to EDIT form.
		$details = $posts->validate('edit', $details);

		/**
		* Build Smarty Variable with FULL details
		*/
		$smarty->assign('posts', $details);
	}
	else
	{
		# Really Bad...
		stopper::url('posts-direct-access-error.php');
	}
}
?>